package my.diplom.web.controllers;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.validation.BindException;
import my.diplom.dao.service.ProjectService;
import my.diplom.dao.service.StatusService;
import my.diplom.dao.service.UserService;
import my.diplom.entity.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.beans.PropertyEditorSupport;

/**
 * Created by IntelliJ IDEA.
 * User: GGobozov
 * Date: 04.08.2009
 * Time: 18:47:28
 * To change this template use File | Settings | File Templates.
 */
public class ProjectController extends SimpleFormController {


    private ProjectService projectService;
    private StatusService statusService;
    private UserService userService;

      public ProjectController(){
        setCommandClass(Project.class);
        setCommandName("newProject");
        setSessionForm(false);
        setBindOnNewForm(false);

    }


    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setProjectService(ProjectService projectService) {
        this.projectService = projectService;
    }

    public void setStatusService(StatusService statusService) {
        this.statusService = statusService;
    }


    protected Map referenceData(HttpServletRequest request) throws Exception {
        List<Project> projects = projectService.getAll();
        List<User> users = userService.getAll();
        HashMap<String, Object> model = new HashMap<String, Object>();
        model.put("users", users);
        model.put("projects", projects);
        return model;
    }

     protected Object formBackingObject(HttpServletRequest request) throws Exception {
        if (request.getParameter("projectId") != null && request.getParameter("action").equals("edit")) {
            Project project = projectService.getById(Long.parseLong(request.getParameter("projectId")));
            return project;
        } else {
            Project project = new Project();
            return project;
        }
    }


    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o, BindException e) throws Exception {
        Project project = (Project) o;
        projectService.create(project);
        return new ModelAndView(new RedirectView("createProject.htm"));
    }

    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
            binder.registerCustomEditor(Status.class, "status", new PropertyEditorSupport() {

              public void setAsText(String text) {
                  if (text instanceof String) {
                      if (text.equals("inactive")) {
                          Status status = statusService.getByName("inactive");
                          setValue(status);
                      } else if (text.equals("active")) {
                          Status status = statusService.getByName("active");
                          setValue(status);
                      }

                  }
              }

              public String getAsText() {
                  Object value = getValue();
                  if (value != null) {
                      Status status = (Status) value;
                      return status.getStatusName();
                  }
                  return null;
              }


          });

    }


    

}
